<template>
    <div>
        <el-dialog title="新增" v-model="isShowDialog" width="600px" @close="closeDialog">
            <el-form ref="formRef" :model="formModel" :rules="formRules" size="default" label-width="120px">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="formModel.userName" show-word-limit maxlength="30" placeholder=""
                        clearable></el-input>
                </el-form-item>
                <el-form-item label="登录名" prop="loginName">
                    <el-input v-model="formModel.loginName" show-word-limit maxlength="100" placeholder=""
                        clearable></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="formModel.password" show-word-limit maxlength="16" placeholder=""
                        clearable></el-input>
                </el-form-item>
                <el-form-item label="用户状态" prop="userStatus">
                    <el-select v-model="formModel.userStatus" placeholder="">
                        <el-option v-for="item in userStatusEnums" :key="item.key" :label="item.label" :value="item.key" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="() => closeDialog()" size="default">取 消</el-button>
                    <el-button type="primary" :loading="submitLoading" @click="onSubmit" size="default">提 交</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { UserInfoAddParamModel } from '/@/interfaces/JdfsystemModel';
import { handleAdd, handleValidateForm } from '../user-info-wrap';
import { DictInfo } from '/@/enums/dict-info-enum';
import UploadImage from '/@/views/file-info/component/upload-image.vue';

interface State {
    isShowDialog: boolean | true,
    submitLoading: boolean | false,
    formModel: UserInfoAddParamModel,
    formRules: { [key: string]: any[] },
    userStatusEnums: any[],
}

export default defineComponent({
    name: 'UserInfoAdd',
    components: {UploadImage},
    emits: ['onClose'],
    setup(props, context) {
        const state = reactive<State>({
            isShowDialog: true,
            submitLoading: false,
            formModel: {
                userName: undefined,
                loginName: undefined,
                password: undefined,
                userStatus: undefined,
            },
            formRules: {
                userName: [{ required: true, message: '用户名不能为空', trigger: 'change' }],
                loginName: [{ required: true, message: '登录名不能为空', trigger: 'change' }],
                password: [{ required: true, message: '密码不能为空', trigger: 'change' }],
                userStatus: [{ required: true, message: '用户状态不能为空', trigger: 'change' }],
            },
            userStatusEnums: DictInfo.userStatus,
        });
        const methodMap = reactive({
            // 关闭弹窗
            closeDialog: function () {
                state.isShowDialog = false;
                context.emit('onClose');
            },
            // 新增
            onSubmit: async function () {
                state.submitLoading = false;
                setTimeout(() => state.submitLoading = false, 1500);
                // 表单验证
                let validate = await handleValidateForm(formRef.value);
                if (!validate) {
                    state.submitLoading = false;
                    return;
                }
                // 提交
                await handleAdd({ param: state.formModel, pageInfo: undefined }, methodMap.closeDialog);
            },

        });
        const formRef = ref<FormInstance>();

        return {

            formRef,
            ...toRefs(state),
            ...toRefs(methodMap),
        };
    },
});
</script>
